// packageOrder/pages/order/order-list.js
import OrderService from '../../../service/OrderService';
import WalletService from '../../../service/WalletService';
import { throttle } from '../../../utils/util.js';
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    buttons:[
      {
        type: 1,
        label: '已发布订单'
      }, {
        type: 2,
        label: '生产中订单'
      }, {
        type: 3,
        label: '已出厂订单'
      }, {
        type: 4,
        label: '质保期订单'
      }, {
        type: 5,
        label: '质保后订单'
      }
    ],
    result: [{list:[], page:{}}],
    type: 1,
    user: app.globalData.user,
    param: {pageNum: 1, pageSize: 10, type: 1}
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.orderService = new OrderService();
    this.walletService = new WalletService();
    const { buttons, param } = this.data;
    if ( app.globalData.user.userType == 1 ){
      buttons[0].label = '已发布订单'
    } else if (app.globalData.user.userType == 2){
      buttons[0].label = '已投标订单'
    }
    this.setData({ buttons });
    this.loadData(param);
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.loadData(this.data.param);
  },

  loadData: function (param){
    const _ = this;
    _.orderService.managerList(param).then(function (res) {
      console.log(res)
      if (res.status == 0) {
        _.setData({ result: res.data })
      }
    })
  },
  onStateChange: function(e){
    const {param} = this.data;
    param.type = e.detail.type;
    param.pageNum = 1;
    this.setData({ param });
    this.loadData(param)
  },
  /**去选厂 */
  toSelectFactory: function(e){
    const { id } = e.target.dataset;
    console.log('aaa')
    throttle(function(){
      wx.navigateTo({
        url: `/packageUser/pages/order/recommend-factory?id=${id}`
      })
    },1000)();
  },
  /**去质量评价 */
  toQualityEval: function(e){
    const {id, factoryId} = e.target.dataset;
    throttle(function(){
      wx.navigateTo({
        url: `/packageUser/pages/evaluate/quality-evaluate?id=${id}&factoryId=${factoryId}`
      })
    },1000)();
  },
  /**去支付评价 */
  toPayEval: function(e){
    const { id, businessId } = e.target.dataset;
    throttle(function () {
      wx.navigateTo({
        url: `/packageUser/pages/evaluate/pay-evaluate?id=${id}&businessId=${businessId}`
      })
    }, 1000)();
  },
  /**退还保金 */
  doBondBack:function(e){
    const _ = this;
    const {param} = this.data;
    const { id } = e.target.dataset;
    throttle(function () {
      _.orderService.bondBack(id).then(function(res){
        console.log(res);
        if(res.status === 0){
          param.pageNum = 1;
          _.loadData(param);
          _.setData({param});
        }
      })
    },1000)();
  },
  /**延期退保 */
  doDelayBack: function(e){
    const _ = this;
    const { param } = this.data;
    const { id } = e.target.dataset;
    throttle(function () {
      _.orderService.delayBack(id).then(function (res) {
        if (res.status === 0) {
          param.pageNum = 1;
          _.loadData(param);
          _.setData({param});
        }
      })
    }, 1000)();
  },
  /**延期退保后，保金退还 */
  doDelayBondBack: function(e){
    const _ = this;
    const { param } = this.data;
    const { id } = e.target.dataset;
    throttle(function () {
      _.orderService.delayBondBack(id).then(function (res) {
        if (res.status === 0) {
          param.pageNum = 1;
          _.loadData(param);
          _.setData({param})
        }
      })
    }, 1000)();
  },
  /**订单出厂操作 */
  doOutFactory:function(e){
    const _ = this;
    const { param } = this.data;
    const { id } = e.target.dataset;
    throttle(function () {
      _.orderService.update({id, orderStatus: 2}).then(function(res){
        if (res.status === 0) {
          param.pageNum = 1;
          _.loadData(param);
          _.setData({param})
        }
      });
    }, 1000)();
  },
  /**申请售后 */
  doAfterSale:function(e){
    const _ = this;
    const { id, factoryId } = e.target.dataset;
    throttle(function () {
      wx.navigateTo({
        url: `/packageOrder/pages/complain/complain?id=${id}&factoryId=${factoryId}&type=1`
      })
    }, 1000)();
  },
  /**我要投诉 */
  doComplain:function(e){
    const _ = this;
    const { id, factoryId } = e.target.dataset;
    throttle(function () {
      wx.navigateTo({
        url: `/packageOrder/pages/complain/complain?id=${id}&factoryId=${factoryId}&type=0`
      })
    }, 1000)();
  },
  /**发红包 */
  doEnvelopSpilt:function(e){
    const _ = this;
    const { id, factoryId } = e.target.dataset;
    throttle(function () {
      _.walletService.orderHasEnvelop(id).then(res=>{
        if(res.status == 0){
          if(!res.data){
            wx.navigateTo({
              url: `/packageUser/pages/envelop/envelop?id=${id}`
            })
          }else{
            wx.showToast({
              title: '红包未领完',
            })
          }
        }else{
          wx.showToast({
            title: '请求出错',
          })
        }
      })
    }, 1000)();
  },
  doUpdateOrder:function(e){
    const { id } = e.target.dataset;
    throttle(function () {
      wx.navigateTo({
        url: `/packageOrder/pages/order/create-order?id=${id}`
      })
    }, 1000)();
  },
  onZbFactory: function(e){
    const { factoryId } = e.target.dataset;
    throttle(function () {
      wx.navigateTo({
        url: `/packageFactory/pages/factory/factory-detail?id=${factoryId}`
      })
    }, 1000)();
  },
  onEvaluate: function(e){
    const { id } = e.target.dataset;
    throttle(function () {
      wx.navigateTo({
        url: `/packageUser/pages/evaluate/evaluate?orderId=${id}`
      })
    }, 1000)();
  },
  onItemTap: function (e){
    const { id } = e.target.dataset;
    throttle(function () {
      wx.navigateTo({
        url: `/packageOrder/pages/order/order-detail?id=${id}`
      })
    }, 1000)();
  }
})